#set table = $tabdict.table
#set Table = $tabdict.Table
#set pk = $tabdict.pk
#set PTable = $tabdict.PTable
#set propertys = $tabdict.propertys
#set type = $tabdict.type
package com.kylin.${type}.${table.replace("_","")}.orm;

import com.kylin.hibernate.BaseHibernateDAO;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * Data access object (DAO) for domain model class Testcompany.
 * @see com.kylin.module.test.orm.Testcompany
 * @author liangzh
 */
public class ${Table}DAO extends BaseHibernateDAO {

    private static final Log log = LogFactory.getLog(${Table}DAO.class);

//    
    public void save(${Table} transientInstance) {
        log.debug("?????? ${Table} instance");
        super.save(transientInstance);
    }
    
	public void delete(${Table} persistentInstance) {
        log.debug("deleting ${Table} instance");
        super.delete(persistentInstance);

    }
    
    public ${Table} findById( java.lang.Long id) {
        log.debug("getting ${Table} instance with id: " + id);
        return (${Table})findById(${Table}.class , id);
    }
    
    
    public List findByExample(${Table} instance) {
        log.debug("finding ${Table} instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.kylin.module.${table}.orm.${Table}")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding ${Table} instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from ${Table} as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}

    public ${Table} merge(${Table} detachedInstance) {
        log.debug("merging ${Table} instance");
        try {
            ${Table} result = (${Table}) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(${Table} instance) {
        log.debug("attaching dirty ${Table} instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(${Table} instance) {
        log.debug("attaching clean ${Table} instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
}